/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ec.dmz.seguridad.ejb.generico;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Estiwar
 */
public class Generico<T> implements Serializable {

    @PersistenceContext(unitName = "DmzSeguridadEjbPU")
    private EntityManager entityManager;
    private Class<T> entityClass;

     public Generico(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    public void create(T entity) {
        getEntityManager().persist(entity);
    }

    public void create(T entity, Object audit) {
        getEntityManager().persist(entity);
        /*AuditoriaGeneral obj = (AuditoriaGeneral) audit;
        obj.setAguComandoejecutado("INSERT INTO " + entity.getClass().getName() + " VALUES (" + entity.toString() + ")");
        obj.setAguAccion("GUARDAR");
        obj.setAguFecharegistro(new Date());
        getEntityManager().persist(obj);*/
    }

    public void edit(T entity) {
        getEntityManager().merge(entity);
    }

    public void edit(T entity, Object audit) {
        getEntityManager().merge(entity);
       // AuditoriaGeneral obj = (AuditoriaGeneral) audit;
       // obj.setAguAccion("ACTUALIZAR");
       // obj.setAguFecharegistro(new Date());
       // obj.setAguComandoejecutado("UPDATE " + entity.getClass().getName() + " SET (" + entity.toString() + ")");
        getEntityManager().persist(audit);
    }

    public void remove(T entity) {
        getEntityManager().remove(getEntityManager().merge(entity));
    }

    public T find(Object id) {
        return getEntityManager().find(entityClass, id);
    }

    public List<T> findAll() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        return getEntityManager().createQuery(cq).getResultList();
    }

    public List<T> findRange(int[] range) {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(entityClass));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        q.setMaxResults(range[1] - range[0]);
        q.setFirstResult(range[0]);
        return q.getResultList();
    }

    public int count() {
        javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
        cq.select(getEntityManager().getCriteriaBuilder().count(rt));
        javax.persistence.Query q = getEntityManager().createQuery(cq);
        return ((Long) q.getSingleResult()).intValue();
    }

    public void flush() {
        this.getEntityManager().flush();
    }

      public void refresh(T entity) {
        this.getEntityManager().refresh(entity);
    }
    
    /**
     * @return the entityManager
     */
    public EntityManager getEntityManager() {
        return entityManager;
    }

    /**
     * @param entityManager the entityManager to set
     */
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }
}
